package com.wf.smartcomm.server.client.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.apache.log4j.Logger;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.aspectj.util.FileUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.Advertisement;
import com.wf.smartcomm.server.bean.Complain;
import com.wf.smartcomm.server.bean.Guide;
import com.wf.smartcomm.server.bean.Image;
import com.wf.smartcomm.server.bean.Shop;
import com.wf.smartcomm.server.bean.Praise;
import com.wf.smartcomm.server.bean.Repair;
import com.wf.smartcomm.server.bean.ThumbImage;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.response.ResponseCode;
import com.wf.smartcomm.server.service.ImageService;
import com.wf.smartcomm.server.service.ThumbImageService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.IdUtils;
import com.wf.smartcomm.server.util.ImageUtils;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.util.StringUtil;


/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-3-24 上午10:47:13 
 * 
 */
@Controller
@Scope("prototype")
public class ImageAction extends BaseAction {
	
	private final Logger logger = Logger.getLogger(ImageAction.class);

	@Resource
	private ImageService imageServiceImpl;
	@Resource
	private ThumbImageService thumbServiceImpl;

	public void save() throws IOException {
		String vid = request.getParameter("vid");
		String cid = request.getParameter("cid");
		String pid = request.getParameter("pid");
		String rid = request.getParameter("rid");
		String gid = request.getParameter("gid");
		String sid = request.getParameter("sid");
		String aid = request.getParameter("aid");
		Village village = null;
		Complain complain = null;
		Praise praise = null;
		Repair repair = null;
		Guide guide = null;
		Shop shop = null;
		Advertisement ad = null;
		if (!StringUtil.isEmpty(vid)) {
			village = new Village();
			village.setObjectId(vid);
		}else if(!StringUtil.isEmpty(cid)){
			complain = new Complain();
			complain.setObjectId(cid);
		}else if(!StringUtil.isEmpty(pid)){
			praise = new Praise();
			praise.setObjectId(pid);
		}else if (!StringUtil.isEmpty(rid)) {
			repair = new Repair();
			repair.setObjectId(rid);
		}else if (!StringUtil.isEmpty(gid)) {
			guide = new Guide();
			guide.setObjectId(gid);
		}else if(!StringUtil.isEmpty(sid)) {
			shop = new Shop();
			shop.setObjectId(sid);
		}else {
			ad = new Advertisement();
			ad.setObjectId(aid);
		}
		
		MultiPartRequestWrapper pr = (MultiPartRequestWrapper) request;
		File[] files = pr.getFiles("fileData");
		if (files != null) {
			List<Image> images = new ArrayList<Image>();
			List<ThumbImage> thumbs = new ArrayList<ThumbImage>();
			String requestUrl = request.getRequestURL().toString();
			for (File file : files) {
				String fileName = IdUtils.uuid()+".png";
				Image image = new Image();
				String path = request.getServletContext().getRealPath(
						"images");
				//String url = requestUrl.substring(0, requestUrl.lastIndexOf("wisdomhome"))+"images/";
				String url = "http://"+PushUtils.pushConfig.getProperty("ImageServerIp");
				if (request.getServerPort() != 80) {
					url+=":"+request.getServerPort();
				}
				url+=request.getContextPath()+"/images/";
				image.setUrl(url + fileName);
				image.setFileName(fileName);
				File des = new File(path + File.separator + fileName);
				FileUtil.copyFile(file, des);
				BufferedImage bufferedImage = ImageIO.read(des);
				int width = bufferedImage.getWidth();   
				int height = bufferedImage.getHeight();
				System.out.println("width:"+width+" height:"+height);
				ThumbImage thumbImage = new ThumbImage(); 
				thumbImage.setFileName(fileName);
				if (village != null) {
					thumbImage.setVillage(village);
				}else if (complain != null) {
					thumbImage.setComplain(complain);
				}else if (praise != null) {
					thumbImage.setPraise(praise);
				}else if (repair != null) {
					thumbImage.setRepair(repair);
				}else if (guide != null) {
					thumbImage.setGuide(guide);
				}else if (shop != null){
					thumbImage.setShop(shop);
				}else {
					thumbImage.setAd(ad);
				}
				thumbImage.setUrl(url.replace("images", "thumbImages") +fileName);
				ImageUtils.getThumbnail(des.getAbsolutePath(),
						request.getServletContext().getRealPath(
								"thumbImages")+File.separator+fileName, 150, 150*height/width);
				images.add(image);
				thumbs.add(thumbImage);
			}
			imageServiceImpl.save(images);
			thumbServiceImpl.save(thumbs);
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		printJsonData(gson.toJson(map));
	}
	
	public void listByComplain() throws IOException {
		String cid = request.getParameter("cid");
		Complain complain = new Complain();
		complain.setObjectId(cid);
		List<ThumbImage> imageList = thumbServiceImpl.listByComplain(complain);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", imageList);
		printJsonData(gson.toJson(map));
	}
	
	public void listByVillage() throws IOException {
		String vid = request.getParameter("vid");
		Village village = new Village();
		village.setObjectId(vid);
		List<ThumbImage> imageList = thumbServiceImpl.listByVillage(village);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", imageList);
		printJsonData(gson.toJson(map));
	}
	
	public void listByPraise() throws IOException {
		String vid = request.getParameter("vid");
		Praise praise = new Praise();
		praise.setObjectId(vid);
		List<ThumbImage> imageList = thumbServiceImpl.listByPraise(praise);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", imageList);
		printJsonData(gson.toJson(map));
	}
	
	public void listByRepair() throws IOException {
		String vid = request.getParameter("vid");
		Repair repair = new Repair();
		repair.setObjectId(vid);
		List<ThumbImage> imageList = thumbServiceImpl.listByRepair(repair);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", imageList);
		printJsonData(gson.toJson(map));
	}
	
	public void listByGuide() throws IOException {
		String vid = request.getParameter("vid");
		Guide guide = new Guide();
		guide.setObjectId(vid);
		List<ThumbImage> imageList = thumbServiceImpl.listByGuide(guide);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", imageList);
		printJsonData(gson.toJson(map));
	}
	
	public void listByShop() throws IOException {
		String vid = request.getParameter("vid");
		Shop shop = new Shop();
		shop.setObjectId(vid);
		List<ThumbImage> imageList = thumbServiceImpl.listByShop(shop);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", imageList);
		printJsonData(gson.toJson(map));
	}
	
	public void listByAdvertisement() throws IOException {
		String vid = request.getParameter("vid");
		Advertisement ad = new Advertisement();
		ad.setObjectId(vid);
		List<ThumbImage> imageList = thumbServiceImpl.listByAdvertisement(ad);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", imageList);
		printJsonData(gson.toJson(map));
	}
}
